//
//  ReleaseConfirm.m
//  jieshuibao
//
//  Created by 边文辉 on 2017/1/14.
//  Copyright © 2017年 com.jieshuibao1. All rights reserved.
//

#import "ReleaseConfirm.h"


@interface ReleaseConfirm ()
{
    NSString *_updateDes;
}
@end

@implementation ReleaseConfirm

static NSString *kReleasVersion = @"releasVersion";

+ (instancetype)DefultInstance
{
    static ReleaseConfirm *_instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[ReleaseConfirm alloc] init];
    });
    
    return _instance;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
        _releaseVersion = [standardUserDefaults integerForKey:kReleasVersion];
        if (!_releaseVersion) {

            self.releaseVersion = kUpdateVersion;

        }
        _showUpdate = NO;
    }
    return self;
}

- (BOOL)releasing
{
//    return (_releaseVersion > 1);
//    [appMPVersion integerValue] < _releaseVersion; // 正常
//    _releaseVersion < [appMPVersion integerValue];  // 审核
    return (_releaseVersion >= kUpdateVersion);
}

- (void)setReleaseVersion:(NSInteger)releaseVersion
{
    _releaseVersion = releaseVersion;
    [[NSUserDefaults standardUserDefaults] setInteger:_releaseVersion forKey:kReleasVersion];
}

- (void)confirm
{
    [RequestTool POST:kVersionUrl parameters:@{@"type":@"1"} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    

        DeLog(@"responseObject %@", responseObject);
        DeLog(@"版本更新请求的数据：respo - %@", responseObject);
        [self parseResponse:responseObject];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            DeLog(@"faild iosUpCode, error %@", error);
        }
        static int retry = 3;
        if (retry--) {
            [self confirm];
        }
    }];
}

- (void)parseResponse:(NSDictionary *)response
{
    
    NSString *code = response[@"code"];
    if (code.integerValue!=1) {
        return;
    }
    NSUInteger updateCode = [response[@"data"][@"iosCode"] integerValue];
    NSString *updateDes = response[@"data"][@"description"];
    DeLog(@"%@",updateDes);
    
    if (![updateDes isKindOfClass:[NSString class]]) {
        _updateDes = @"上线新功能";
    }else {
        _updateDes = updateDes;
    }
    if (updateCode != _releaseVersion) {
        self.releaseVersion = updateCode;
    }
    
    if (_releaseVersion > kUpdateVersion) {
        _showUpdate = YES;
//        [self showUpdateAlert];
    }
    [self showUpdateAlert];
    
}


- (void)showUpdateAlert
{
    if (!_showUpdate || ![BIZUserModel shareUser].login) return;
    _showUpdate = NO;
     NSString *tempStr = [_updateDes stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:tempStr preferredStyle:
                                UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleCancel handler:nil]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E8%A7%A3%E7%A8%8E%E5%AE%9D%E4%BC%81%E4%B8%9A%E7%89%88/id1230403452?mt=8"];
        UIApplication *sharedApplication = [UIApplication sharedApplication];
        if (iOS10Later) {
            [sharedApplication openURL:url options:@{} completionHandler:nil];
        } else {
            [sharedApplication openURL:url];
        }
    }]];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    
}


@end
